//
//  LZLoginView.swift
//  halikaPeso
//
//  Created by L-zh on 2024/12/18.
//

import UIKit

class LZLoginView: LZBaseView {

    private lazy var backgroundImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "log in_bg"))
        imageView.contentMode = .scaleAspectFill
        return imageView
    }()
    
    private lazy var logoImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "big logo bg"))
        imageView.contentMode = .scaleAspectFit //不拉伸
        return imageView
    }()
    
    private lazy var inputImageViewBg: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "log in_bg_01"))
        imageView.contentMode = .scaleAspectFit //不拉伸
        return imageView
    }()
    
    private lazy var blackTitleLbl: UILabel = {
        let label = UILabel()
        label.text = "Enter Your Phone Number"
        label.font = UIFont.boldSystemFont(ofSize: 17)
        label.textColor = UIColor.hexcolor(withHex: 0x151A09)
        label.textAlignment = .center
        return label
    }()
    
    private lazy var flagImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "flag bg"))
        imageView.contentMode = .scaleAspectFit //不拉伸
        imageView.translatesAutoresizingMaskIntoConstraints = false //不会自动调整尺寸
        return imageView
    }()
    
    private lazy var flagTitleLbl: UILabel = {
        let label = UILabel()
        label.text = "+63"
        label.font = UIFont.boldSystemFont(ofSize: 12)
        label.textColor = UIColor.hexcolor(withHex: 0x151A09)
        label.textAlignment = .center
        return label
    }()
    
    private lazy var textField: UITextField = {
       let textField = UITextField()
        textField.placeholder = "Phone Number"
        textField.font = .systemFont(ofSize: 15)
        textField.borderStyle = .roundedRect
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.keyboardType = .phonePad
        return textField
    }()
    
    private lazy var sendButton: UIButton = {
        let stackView = UIStackView()
        stackView.isUserInteractionEnabled = false // 确保所有子视图不拦截触摸事件
        stackView.axis = .horizontal
        stackView.alignment = .center //水平居中对齐
        stackView.distribution = .fill //分布方式为填充
        //设置元素间的间距
        stackView.spacing = 2
        stackView.isLayoutMarginsRelativeArrangement = true
        stackView.layoutMargins = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        
        //左边图标
        let leftImageView = UIImageView(image: UIImage(named: "send bg"))
        leftImageView.contentMode = .scaleAspectFit
        leftImageView.snp.makeConstraints { make in
            make.width.equalTo(25)
        }
        
        //中间文字
        let label = UILabel()
        label.text = "Send Code"
        label.textAlignment = .center
        label.textColor = .white
        label.font = .boldSystemFont(ofSize: 17)
        
        //右边图标
        let rightImageView = UIImageView(image: UIImage(named: "send right bg"))
        rightImageView.contentMode = .scaleAspectFit
        rightImageView.snp.makeConstraints { make in
            make.width.equalTo(16)
        }
        
        //将图标和文本添加到堆栈识图
        [leftImageView, label, rightImageView].forEach(stackView.addArrangedSubview)
        
        //创建按钮并添加到堆栈识图
        let button = UIButton(type: .custom)
        button .addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalTo(button).inset(UIEdgeInsets(top: 15, left: 85, bottom: 15, right: 85))
        }
        
//        button.setTitle("Send Code", for: .normal)
//        button.setImage(UIImage(named: "send bg"), for: .normal) //设置图标
//        button.imageView?.contentMode = .scaleAspectFit //确保图标按比例缩放
//        // 设置图标并保持原始颜色
//        if let image = UIImage(named: "send bg")?.withRenderingMode(.alwaysOriginal){
//            button.setImage(image, for: .normal)
//        }
        button.addTarget(self, action: #selector(sendButtonClick), for: .touchUpInside)
        button.backgroundColor = UIColor.hexcolor(withHex: 0x34CD67)
//        button.setTitleColor(.white, for: .normal)
//        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
//        // 调整图标和文本的位置
//        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -4, bottom: 0, right: 4)
//        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: -4)
        // 增加一些额外的空间以确保图标和文本之间有足够的间距
//        button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        button.layer.cornerRadius = 8
        button.layer.borderWidth = 1
        return button
    }()
    
    private lazy var gouImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "log in_ic_01")
        imageView.highlightedImage = UIImage(named: "log in_ic_02")
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    //同意协议
    private lazy var agreeConsentButton: UIButton = {
        
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.isUserInteractionEnabled = false // 确保所有子视图不拦截触摸事件
        stackView.alignment = .center //水平居中对齐
        stackView.distribution = .fill //分布方式为填充
        //设置元素间的间距
        stackView.spacing = 2
        stackView.isLayoutMarginsRelativeArrangement = true
        stackView.layoutMargins = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
        
        //左边图标
        gouImageView.snp.makeConstraints { make in
            make.width.equalTo(15)
        }
        
        //中间文字
        let label = UILabel()
        label.text = "Log in and agree to the Privacy Policy"
        label.textAlignment = .center
        label.textColor = .white
        label.font = .boldSystemFont(ofSize: 14)
//        label.setupAttributeString(text: label.text, changeText: "Privacy Policy", color: UIColor.hexcolor(withHex: 0x31CF69))
        
        
        // 创建 NSMutableAttributedString 以便修改特定范围内的属性
        let attributedString = NSMutableAttributedString(string: "Log in and agree to the Privacy Policy")
        // 定义公共属性（如字体）
        let commonAttributes: [NSAttributedString.Key: Any] = [
            .font: UIFont.systemFont(ofSize: 13, weight: .bold),
            .strokeWidth: -3.0 // 描边加填充
        ]
        
        // 应用公共属性到整个字符串
        attributedString.addAttributes(commonAttributes, range: NSRange(location: 0, length: attributedString.length))
        
//        let range = label.nsRanges(for: "Log in and agree to the", in: label.text ?? String())
        // 设置 "01" 的属性（白色填充，黑色描边）
        attributedString.addAttributes([
            .foregroundColor: UIColor.white,
            .strokeColor: UIColor.black
        ], range: label.getNSRange(for: "Log in and agree to the", in: label.text!) ?? NSRange())
        
        // 设置 "23" 的属性（绿色填充，黑色描边）
        attributedString.addAttributes([
            .foregroundColor: UIColor.green,
            .strokeColor: UIColor.black
        ], range: label.getNSRange(for: "Privacy Policy", in: label.text!) ?? NSRange())
        
         //将 NSAttributedString 应用到 UILabel
        label.attributedText = attributedString
        
        //将图标和文本添加到堆栈识图
        [gouImageView, label].forEach(stackView.addArrangedSubview)
        
        //创建按钮并添加到堆栈识图
        let button = UIButton(type: .custom)
        button .addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalTo(button).inset(UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5))
        }
        
//        button.setTitle("Send Code", for: .normal)
//        button.setImage(UIImage(named: "send bg"), for: .normal) //设置图标
//        button.imageView?.contentMode = .scaleAspectFit //确保图标按比例缩放
//        // 设置图标并保持原始颜色
//        if let image = UIImage(named: "send bg")?.withRenderingMode(.alwaysOriginal){
//            button.setImage(image, for: .normal)
//        }
        button.addTarget(self, action: #selector(agreeConsentButtonClick), for: .touchUpInside)
        button.backgroundColor = UIColor.hexcolor(withHex: 0xFFE63C)
//        button.setTitleColor(.white, for: .normal)
//        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
//        // 调整图标和文本的位置
//        button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -4, bottom: 0, right: 4)
//        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: -4)
        // 增加一些额外的空间以确保图标和文本之间有足够的间距
//        button.contentEdgeInsets = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        button.layer.cornerRadius = 8
        button.layer.borderWidth = 1
        return button
    }()
    
    override init(frame: CGRect, viewController: LZBaseVC) {
        super.init(frame: frame, viewController: viewController)
        setUpdateConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpdateConstraints() {
        addSubview(backgroundImageView)
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        addSubview(logoImageView)
        logoImageView.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.width.equalTo(APPScale(f: 215))
            make.height.equalTo(APPScale(f: 190))
            make.top.equalTo(self).offset(APPScale(f: 100))
        }
        
        addSubview(inputImageViewBg)
        inputImageViewBg.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.width.equalTo(APPScale(f: 336))
            make.height.equalTo(APPScale(f: 223))
            make.top.equalTo(logoImageView.snp.bottom).offset(APPScale(f: 14))
        }
        
        inputImageViewBg.addSubview(blackTitleLbl)
        blackTitleLbl.snp.makeConstraints { make in
            make.top.leading.trailing.equalTo(inputImageViewBg).inset(UIEdgeInsets(top: 14, left: 66, bottom: 0, right: 66))
            make.height.equalTo(17)
        }
        
        inputImageViewBg.addSubview(flagImageView)
        flagImageView.snp.makeConstraints { make in
            make.width.equalTo(24)
            make.height.equalTo(16)
            make.left.equalTo(inputImageViewBg).offset(15)
            make.top.equalTo(inputImageViewBg).offset(80)
        }
        
        inputImageViewBg.addSubview(flagTitleLbl)
        flagTitleLbl.snp.makeConstraints { make in
            make.height.equalTo(12)
            make.left.right.equalTo(flagImageView)
            make.top.equalTo(flagImageView.snp.bottom).offset(2)
        }
        
        inputImageViewBg.addSubview(textField)
        textField.snp.makeConstraints { make in
            make.height.equalTo(46)
            make.top.leading.trailing.equalTo(inputImageViewBg).inset(UIEdgeInsets(top: 72, left: 49, bottom: 0, right: 16))
        }
        
        inputImageViewBg.addSubview(sendButton)
        sendButton.snp.makeConstraints { make in
            make.height.equalTo(APPScale(f: 46))
            make.bottom.leading.trailing.equalTo(inputImageViewBg).inset(UIEdgeInsets(top: 0, left: 15, bottom: 30, right: 15))
        }
        
        addSubview(agreeConsentButton)
        agreeConsentButton.snp.makeConstraints { make in
            make.height.equalTo(42)
            make.top.equalTo(inputImageViewBg.snp.bottom).offset(APPScale(f: 20))
            make.leading.trailing.equalTo(self).inset(UIEdgeInsets(top: 0, left: 48, bottom: 0, right: 48))
        }
    }
    
    @objc private func sendButtonClick() {
        print("sendButton",(UIDevice.current.systemVersion))
    }
    
    @objc private func agreeConsentButtonClick() {
        print("agreeConsentButtonClick",(UIDevice.current.systemVersion))
    }
}
